﻿package net.jmp0.toast.entities.effects 
{
	import flash.geom.ColorTransform;
	import net.jmp0.toast.entities.bases.GameButton;
	import net.jmp0.toast.levels.GameLevel;
	import net.jmp0.toast.levels.LevelTracker;
	import punk.Acrobat;
	import punk.core.Spritemap;
	import punk.Text;
	import punk.util.Input;
	/**
	 * ...
	 * @author Thodd
	 */
	public class MessageBox extends Acrobat	{
		
		//Static Constants used to describe the button scheme
		public static const TYPE_OK_AND_CANCEL:int = 0;
		public static const TYPE_OK:int = 1;
		
		//Static constants indicating which button was clicked
		public static const CLICKED_OK:int = 0;
		public static const CLICKED_CANCEL:int = 1;
		
		//Embedding the toast face sprite and the speech bubble sprite
		[Embed(source = '../../res/orangeBox.png')] private var imgOrangeBox:Class;
		private var sprOrangeBox:Spritemap = FP.getSprite(imgOrangeBox, 400, 250, false, false, 7, 7, false);
		
		[Embed(source = '../../res/toastFaceAnimated.png')] private var imgToastFace:Class;
		private var sprToastFace:Spritemap = FP.getSprite(imgToastFace, 100, 100, false, false, 0, 0, false);
		
		//The text inside the speech bubble
		private var msg:GameText;
		
		//the button scheme
		private var msgType:int = 1;
		
		//this function is called by msgbox the time when one of it's buttons was pressed
		private var callback:Function = null;
		
		//The drawing position
		private var drawX:int = 0;
		private var drawY:int = 0;
		
		//buttons
		private var buttonOK:GameButton;
		private var buttonCancel:GameButton;
		
		public function MessageBox(text:String, x:int, y:int, msgboxType:int, callback:Function) {
			//basic setup
			this.x = x;
			this.y = y;
			this.depth = -300;
			this.delay = (30 / 3);
			
			//this is the function the msgbox is going to call when a button is pressed/the msgbox is closed
			this.callback = callback;
			
			calculateDrawingPosition();
			
			setupTheSpeechBubble(text);
			
			setupTheButtons(msgboxType);
			
			FP.world.add(this);
			
		}
		
		
		/**
		 * Used to make the speech bubble lock nice :p
		 * @param	text the Text inside the speech bubble
		 */
		private function setupTheSpeechBubble(text:String):void {
			//setup speech bubble sprite
			var colorT:ColorTransform = new ColorTransform();
			colorT.alphaMultiplier = 0.7;
			sprOrangeBox.colorTransform(sprOrangeBox.rect, colorT);
			
			//setup the text object
			msg = new GameText(text, x, y, "garbage", 26, -301, true);
			msg.color = 0x000000;
			
			//Positioning the text inside the speechbubble
			msg.x = drawX+22;
			msg.y = drawY+14;
		}
		
		
		/**
		 * used to create the buttons according to the msgboxType
		 * @param	msgboxType The type of msgbox, OK or OK_AND_CANCEL
		 */
		private function setupTheButtons(msgboxType:int):void {
			if (msgboxType == TYPE_OK) {
				buttonOK = new GameButtonOK(drawX+260, drawY+85, transmitButtonClick);
			}else {
				buttonOK = new GameButtonOK(drawX+200, drawY+85, transmitButtonClick);
				buttonCancel = new GameButtonCancel(drawX+260, drawY+85, transmitButtonClick);
			}
		}
		
		
		/**
		 * Sets drawX and drawY
		 * If the player clicked in the lower right, the MsgBox appears in the upper left and so on
		 */
		private function calculateDrawingPosition():void {
			if(x >= 220){
				drawX = 15;
			}else{
				drawX = 260;
			}
			
			if(y >= FP.screen.height/2){
				drawY = 50;
			}else{
				drawY = 240;
			}
			
		}
		
		/**
		 * Callback function for a GameButton. This function is called by the GameButton the time it was clicked
		 * Like a "MouseClicked-Event"
		 * 
		 * @param	caller The Button which was clicked
		 */
		private function transmitButtonClick(caller:GameButton):void {
			//determine what button called this method... respectively which button was clicked
			if (caller is GameButtonOK) {
				callback(MessageBox.CLICKED_OK);
			}else if (caller is GameButtonCancel) {
				callback(MessageBox.CLICKED_CANCEL);
			}
			//and then destroying itself
			this.destroy();
		}
			
		
		/**
		 * Destroys the msgbox and takes care of cleaning up
		 */
		public function destroy():void {
			trace("msgbox destroyed");
			
			//Cleaning up
			if(buttonOK != null){
				buttonOK.destroy();
			}
			if (buttonCancel != null) {
				buttonCancel.destroy();
			}
			
			msg.destroy();
			
			FP.world.remove(this);
		}
		
		
		override public function render():void {
			updateImage(sprToastFace.number);
			drawSprite(sprOrangeBox, 0, drawX, drawY, false, false); //the speech bubble is slightly transparent
			drawSprite(sprToastFace, image, drawX, drawY + 140, false, false); //the toast is solid, drawn relative to the speech bubble
		}
		
	}

}